<?php
add_action('widgets_init', 'register_unpopular_coupon_widget');

function register_unpopular_coupon_widget()
{
	register_widget('unpopular_coupon');
}
class unpopular_coupon extends WP_Widget // widget class
{
	function unpopular_coupon() // widget setting | class name must be same function name
	{
		$widget_ops = array('classname' => 'unpopular-coupon-w', 'description' =>
				'Display unpopular coupons - Store detail page');
		$control_ops = array(
			'width' => 300,
			'height' => 350,
			'id_base' => 'unpopular_coupon');
		$this->WP_Widget('unpopular_coupon', 'CouponX - Unpopular coupons', $widget_ops, $control_ops);
	}
	function widget($args, $instance) // display widget
	{
		extract($args);
        
        global $post;
        if($post)
            $store_id = $post->ID;
		
		$title = apply_filters('widget_title', $instance['title']);		
		$itemsToShow = $instance['number_show'];
        if(!$itemsToShow)
            $itemsToShow = 10;
        $show_expired = $instance['show_expire'];        
        $rate_smaller = $instance['rate_smaller'];
        if(!$rate_smaller)
            $rate_smaller = 50;            
            
        if($store_id) // Display in store detail page
        {
       	    $d = date('y/m/d');            
            $rs = cpx_get_unpopular_coupons($store_id, $d);
            
        	if (count($rs) > 0)
        	{ ?>
                <div class="boxf">
                    <div class="row-fluid un-popular header-coupon">
                        <div class="span12 code-coupon">
                            <h2 class="un-popular"><?php echo strtoupper($title); ?></h2><p class="un-popular">These might not work, but give 'em a try</p> 
                        </div>
                    </div>
         <?php
        		foreach ($rs as $v)
        		{
        			$cp_id = $v->ID;
                    $comment_count = wp_count_comments($cp_id);
                    cpx_get_coupon_detail($cp_id, $comment_count->total_comments);        
        		} ?>
                </div>
          <?php
        	}
        }
	}
    
	function update($new_instance, $old_instance) // update widget
	{
		$instance = $old_instance;		
        $instance['title'] = $new_instance['title'];
        $instance['number_show'] = $new_instance['number_show'];
        $instance['show_expire'] = $new_instance['show_expire'];
        $instance['rate_smaller'] = $new_instance['rate_smaller'];
		return $instance;
	}
    
	function form($instance) // form for the widget options
	{
?>
        <div style="color: #333;">
    		<p>        
    			<label for="<?php echo $this->get_field_id('title');?>"><?php echo 'Title'; ?></label>
    			<input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php	echo $this->get_field_name('title');?>" value="<?php echo $instance['title'];?>" style="width:100%;" />
    		</p>
            <p>
    			<label for="<?php echo $this->get_field_id('number_show');?>"><?php echo 'Number of coupons to show (default 10)';?></label>
    			<input type="text" id="<?php echo $this->get_field_id('number_show');?>" name="<?php echo $this->get_field_name('number_show');?>" value="<?php echo $instance['number_show'];?>" style="width:10%;" />
    		</p>
            <p>
                <label><?php echo 'Show expired coupons'; ?>
                <input type="checkbox" <?php checked( (bool)$instance['show_expire'], true ); ?> id="<?php echo $this->get_field_id('show_expire');?>" name="<?php echo $this->get_field_name('show_expire');?>" /></label>
                
            </p>
            <p>
                <label><?php echo 'Show coupons with rate smaller (default 50%)'; ?></label>
                <input type="text" id="<?php echo $this->get_field_id('rate_smaller');?>" name="<?php echo $this->get_field_name('rate_smaller');?>" value="<?php echo $instance['rate_smaller'];?>" style="width: 10%;" /> %
            </p>	     
        </div>
<?php
	}
}
?>
